import { getStorage, setStorage } from '@/common/auth.js'
import { userLogin } from '@/common/api.js'
//#ifdef H5
const token = window.localStorage.getItem('pro_token') || '';
//#endif

//#ifdef MP-WEIXIN
const token = getStorage("token") || '';
//#endif
//#ifdef APP
const token = uni.getStorageSync('token');
//#endif
const state = {
    hasLogin: token ? true : false,  //是否登陆
    token: token, //toekn
    addressInfo: getStorage('address_key'),
    indexSub: getStorage('indexSub_key'), //首页选中的课程
    //questionsSub: getStorage('questionsSub_key'), //题库选中的课程
    //courses: getStorage('courses_key'), // 用户课程
    userInfo: getStorage('userInfo_key'), // 用户信息
   // orgInfo: getStorage('orgInfo_key'), // 机构信息
    headerName: getStorage('headerName_key'), //标题
    pairStatu: getStorage('pairStatu_key'), //配对信息
	modelType: getStorage('modelType_key'), //设备类型
}
const mutations = {
    SET_TOKEN(state, token) {
        state.token = token
        setStorage('token_key', token)
    },
    SET_USER_CITY(state, address) {
        state.addressInfo = address
        setStorage('address_key', address)
    },
   /* SET_USER_COURSES(state, param) {
        state.courses = param
        setStorage('courses_key', state.courses)
    }, */
    SELECT_FOR_INDEX(state, index) {
        state.indexSub = index
        setStorage('indexSub_key', index)
    },
   /* SELECT_FOR_QUESTIONS(state, index) {
        state.questionsSub = index
        setStorage('questionsSub_key', index)
    }, */
    SET_USER_INFO(state, index) {
        state.userInfo = index
        setStorage('userInfo_key', index)
    },
    /* SET_ORG_INFO(state, index) {
        state.orgInfo = index
        setStorage('orgInfo_key', index)
    }, */
    SET_HEADER_NAME(state, index) {
        state.headerName = index
        setStorage('headerName_key', index)
    },
	SET_PAIR_STATU(state, index) {
	    state.pairStatu = index
	    setStorage('pairStatu_key', index) //配对信息
	},
	SET_MODEL_TYPE(state, index) {
	    state.modelType = index
	    setStorage('modelType_key', index) //设备类型
	},
}
const actions = {
    setToken({ commit }, param) {
        commit('SET_TOKEN', param)
    },
    setUserAddress({ commit }, param) {
        commit('SET_USER_CITY', param)
    },
    setUserInfo({ commit }, param) { //用户信息
        commit('SET_USER_INFO', param)
    },
   /* setOrgInfo({ commit }, param) { //机构信息
        commit('SET_ORG_INFO', param)
    }, */
    /* setCourses({ commit }, param) { //课程
        commit('SET_USER_COURSES', param)
        // 初始话选中下标
        commit('SELECT_FOR_INDEX', 0)
        commit('SELECT_FOR_QUESTIONS', 0)
    }, */
    selectForIndex({ commit }, param) { //选中课程
        commit('SELECT_FOR_INDEX', param)
    },
    /* selectForQuestion({ commit }, param) {
        commit('SELECT_FOR_QUESTIONS', param.index)
    }, */
    setHeaderName({ commit }, param) { //标题
        commit('SET_HEADER_NAME', param)
    },
	setPairStatu({ commit }, param) { //配对信息
	    commit('SET_PAIR_STATU', param)
	},
	setModelType({ commit }, param) { //设备类型
	    commit('SET_MODEL_TYPE', param)
	},
    //登陆
    login(state) {
        handleLogin(state);
    },
}
function handleLogin(state) {
    //#ifdef H5
    // h5登陆 -> 发送url给后台，后端把token 写在本地localStorage中
    console.log('login', state)
    const URI = encodeURIComponent(window.location.href);
    console.log(123123,URI)
    const share_key = window.localStorage.getItem('share_key')
    const os = window.localStorage.getItem('os')
    window.location.href = `${window.appConfig.apiUrl}User.login/login?os=${os}&re_uri=${URI}&from_type=1&login_type=8&share_key=axx`;
    state.hasLogin = true
    // handleAddcar()
    //#endif
    //#ifdef MP-WEIXIN
    uni.login({
        success(loginRes) {
            const { code } = loginRes
            const login_type = 8
            // userLogin({ code, login_type }).then(({data}) => {
            // 	const { token } = data;
            // 	state.token = token
            //     state.hasLogin = true
            //     uni.navigateTo({
            //         url: `/pages/public/tologin`
            //     });

            // })
            uni.request({
                url: 'http://edubak.jstxm.cn/wechat/User.login/login', //仅为示例，并非真实接口地址。
                data: {
                    login_type: login_type,
                    code: code,
                    share_key: '',
                    from_type:1
                },
                dataType: 'json',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: (res) => {
                    alert('授权')
                    console.log('res', res.data);
                    const token = res.data.data.token;
                    state.state.token = token
                    setStorage('token', token)
                    state.hasLogin = true
                    uni.navigateTo({
                        url: `/pages/public/tologin`
                    });
                    //this.text = 'request success';
                }
            });
        },
        fail(e) {
            console.log('errer->', e)
        }
    });
    //#endif
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}

